package com.gao.hash.algorithm;

/**
 * ketama 哈希算法
 */
public class KetamaHashAlgorithm extends AbstractHashAlgorithem implements HashAlgorithm {

    @Override
    public Long hash(String key) {
        return hashKey(key);
    }

    @Override
    protected Long hashKey(String key) {
        byte[] digest = computeMd5(key);
        // hash code, Truncate to 32-bits
        long hashCode = ((long) (digest[3] & 0xFF) << 24)
                | ((long) (digest[2] & 0xFF) << 16)
                | ((long) (digest[1] & 0xFF) << 8)
                | (digest[0] & 0xFF);

        return hashCode & 0xffffffffL;
    }
}
